package psn.zjk.common.poi;

import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTMarker;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineStyleList;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;

import javax.swing.border.Border;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ExcelBuilder {
    XSSFWorkbook wb;
    XSSFSheet currentSheet;
    int nextRowIndex;
    int nextColumnIndex;

    public ExcelBuilder() {
        wb = new XSSFWorkbook();
        currentSheet = wb.createSheet();
    }

    public void create() {

    }

    public static void main(String[] args) throws IOException, InterruptedException {
        ReentrantLock reentrantLock = new ReentrantLock();
        Condition condition = reentrantLock.newCondition();
        reentrantLock.lock();
        condition.await();
        condition.await();
//Create some data to build the pivot table on
        ExcelBuilder excelBuilder = new ExcelBuilder();
        Random random = new Random();
        XSSFSheet currentSheet = excelBuilder.getCurrentSheet();
        XSSFWorkbook wb = currentSheet.getWorkbook();
//        excelBuilder.insertRow("姓名", "年龄", "性别");
//        excelBuilder.insertRow("张三", 11, "male");
//        excelBuilder.insertRow("王五", 41, "male");
//        excelBuilder.insertRow("李四", 21, "female");
        XSSFSheet sheet = excelBuilder.getCurrentSheet();
//        XSSFRow row = sheet.createRow(2);
//        XSSFCell cell1 = row.createCell(2);
//        XSSFCellStyle cellStyle1 = wb.createCellStyle();
//        cellStyle1.setRotation((short) 90);
//        cellStyle1.setWrapText(true);
//        cell1.setCellStyle(cellStyle1);
//        cell1.setCellValue("xxxxxxxxxxxx");

//        row.createCell(3);
//        XSSFCell x = row.getCell(3);
//        XSSFCellStyle cellStyle = wb.createCellStyle();
//        cellStyle.setAlignment(HorizontalAlignment.DISTRIBUTED);
//        cellStyle.setRotation((short) 30);
//        x.setCellStyle(cellStyle);

        XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
        XSSFClientAnchor anchor = drawingPatriarch.createAnchor(0, 0, 0, 0, 0, 0, 11, 11);
        XSSFShapeGroup group = drawingPatriarch.createGroup(anchor);
//        group.setCoordinates();
        XSSFTextBox textbox = group.createTextbox(new XSSFChildAnchor(1,255,5,5));
        textbox.setText("aaaaaaaaaaaa");
        textbox.setVerticalAlignment(VerticalAlignment.TOP);
        textbox.setFillColor(11,32,221);
        XSSFSimpleShape simpleShape = group.createSimpleShape(new XSSFChildAnchor(55, 55,  7, 4));
        simpleShape.setShapeType(ShapeTypes.LINE);
        simpleShape.setLineStyle(0);
        simpleShape.setLineStyleColor(0,0,0);
        simpleShape.setLineWidth(1);
        simpleShape.setFillColor(0,0,0);
        XSSFTextBox textbox1 = group.createTextbox(new XSSFChildAnchor(55,55,2,11));
        textbox1.setText("zzzzzzzz");
//        textbox1.setVerticalAlignment(VerticalAlignment.BOTTOM);
        textbox1.setFillColor(11,32,221);
        File file = new File("xx.xlsx");
//        if (file.exists()) {
//            file.delete();
//        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        sheet.getWorkbook().write(fileOutputStream);
    }

    //透视图
    private void createPivoTtable(XSSFSheet sheet) {
        XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:C3", SpreadsheetVersion.EXCEL2007), new CellReference("H5"));
//Configure the pivot table
//Use first column as row label
        pivotTable.addRowLabel(0);
//Sum up the second column
        pivotTable.addColumnLabel(DataConsolidateFunction.MAX, 1);
//Set the third column as filter
        pivotTable.addColumnLabel(DataConsolidateFunction.PRODUCT, 2);
//        Add filter on forth column
        pivotTable.addReportFilter(0);
    }

    private ExcelBuilder insertRow(Object... cells) {
        if (cells == null || cells.length == 0) {
            return this;
        }
        XSSFRow row = currentSheet.createRow(nextRowIndex++);
        int colIndex = row.getLastCellNum();
        colIndex = colIndex < 0 ? 0 : colIndex;
        for (Object value : cells) {
            XSSFCell cell = row.createCell(colIndex++);
            setCellValue(cell, value);
        }

        return this;
    }

    private void setCellValue(XSSFCell cell, Object value) {
        if (value == null) {
            return;
        }
        if (value instanceof String) {
            cell.setCellValue((String) value);
        } else if (value instanceof RichTextString) {
            cell.setCellValue((RichTextString) value);
        } else if (value instanceof Double) {
            cell.setCellValue((double) value);
        } else if (value instanceof Float) {
            cell.setCellValue((float) value);
        } else if (value instanceof Integer) {
            cell.setCellValue((int) value);
        } else if (value instanceof Date) {
            cell.setCellValue((Date) value);
        } else {
            throw new RuntimeException("not supported type: " + value.getClass().getName());
        }
    }

    public XSSFSheet getCurrentSheet() {
        return currentSheet;
    }
}
